/**
 * <b>项目名称：</b>网络应用框架<br/>
 * <b>包    名：</b>com.hhwy.framework.configure.service<br/>
 * <b>文 件 名：</b>BasicConfigureFromProp.java<br/>
 * <b>版本信息：</b><br/>
 * <b>日    期：</b>2016年8月12日-上午10:04:39<br/>
 * <b>Copyright (c)</b> 2016恒华伟业科技股份有限公司-版权所有<br/>
 * 
 */
package com.hhwy.framework.configure.service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.hhwy.framework.cache.Cache;
import com.hhwy.framework.util.CaseInsensitiveMap;
import com.hhwy.framework.util.IoTools;

 /**
 * <b>类 名 称：</b>BasicConfigureFromProp<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>jlx<br/>
 * <b>修 改 人：</b>jlx<br/>
 * <b>修改时间：</b>2016年8月12日 上午10:04:39<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
public class BasicConfigureLoader {

	private static Logger log = LoggerFactory.getLogger(BasicConfigureLoader.class);
	
	private static final String DRIVER_FIELD="ds.driver";
	
	private static final String URL_FIELD="ds.url";
	
	private static final String UNAME_FIELD="ds.username";
	
	private static final String PASS_FIELD="ds.password";
	
	private Map<String, String> map=new CaseInsensitiveMap<String>();
	
	private BasicConfigureLoader(){}
	
	private static BasicConfigureLoader loader;
	
	public static BasicConfigureLoader getInst(){
		
		if (loader==null) {
			loader=new BasicConfigureLoader();
		}
		
		return loader;
	}
	
	public void loadSystemConfigureFromDB() {
		
		map.clear();
		
		try {
			String driver=Cache.getSystemConfig(DRIVER_FIELD);
			String url=Cache.getSystemConfig(URL_FIELD);
			String uname=Cache.getSystemConfig(UNAME_FIELD);
			String upass=Cache.getSystemConfig(PASS_FIELD);
			
			SystemConfigureFromDB fromdb = new SystemConfigureFromDB(driver,url, uname, upass);
			Map<String, String> dbconfigs = fromdb.loadConfig();
			
			for (String key : dbconfigs.keySet()) {
				
				if (!map.containsKey(key)) {
					map.put(key, dbconfigs.get(key));
				}
			}
			
			configToEhcached(map);
			
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("获取配置信息错误"+e.getMessage());
		}
		
	}
	
//	public void loadLanguageConfigureFromDB() {
//
//		try {
//
//			String driver=Cache.getSystemConfig(DRIVER_FIELD);
//			String url=Cache.getSystemConfig(URL_FIELD);
//			String uname=Cache.getSystemConfig(UNAME_FIELD);
//			String upass=Cache.getSystemConfig(PASS_FIELD);
//
//			LanguageConfigureFromDB fromdb = new LanguageConfigureFromDB(driver,url, uname, upass);
//			Map<String, String> dbconfigs = fromdb.loadConfig();
//			languageConfigToEhcached(dbconfigs);
//		} catch (Exception e) {
//			e.printStackTrace();
//			log.error("获取语言配置信息错误"+e.getMessage());
//		}
//	}
	
	/**
	 * languageConfigToEhcached(描述这个方法的作用)<br/>
	 * @param dbconfigs 
	 * void
	 * @exception 
	 * @since  1.0.0
	*/
//	private void languageConfigToEhcached(Map<String, String> configs) {
//		try {
//			Iterator<String> it = configs.keySet().iterator();
//			while (it.hasNext()) {
//				String key = String.valueOf(it.next());
//				Cache.setLanguageConfig(key, configs.get(key));
//				log.info("--------language cache::key==" + key + " value="+ configs.get(key));
//			}
//
//		} catch (Throwable e) {
//			e.printStackTrace();
//			log.error("SystemConfigure.loadLanguageConfig has error", e);
//		}
//	}

	
	public static void main(String[] args) {
		
		System.out.println(System.getenv(DRIVER_FIELD));
		
		System.out.println(System.getenv());
	}
	
	private void configToEhcached(Map<String, String> configs) {
		try {

			Iterator<String> it = configs.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next().toString();
				Cache.setSystemConfig(key, configs.get(key));
				log.info("--------new cache::key==" + key + " value="+ configs.get(key));
			}

		} catch (Throwable e) {
			e.printStackTrace();
			log.error("SystemConfigure.loadSystemConfig has error", e);
		}

	}
}
